home *** CD-ROM | disk | FTP | other *** search
- Path: goanna.cs.rmit.EDU.AU!not-for-mail
- From: ok@goanna.cs.rmit.EDU.AU (Richard A. O'Keefe)
- Newsgroups: comp.lang.ada,comp.lang.c++,comp.lang.c,comp.lang.modula3,comp.lang.modula2
- Subject: Re: Hungarian notation - whoops!
- Date: 7 Mar 1996 19:20:53 +1100
- Organization: Comp Sci, RMIT, Melbourne, Australia
- Message-ID: <4hm695$na8@goanna.cs.rmit.EDU.AU>
- References: <30C40F77.53B5@swsbbs.com> <4h6hlo$hqu@goanna.cs.rmit.EDU.AU> <4h7vgdINNmsh@anvil.ugrad.cs.ubc.ca> <4hgd11$4p4@goanna.cs.rmit.EDU.AU> <4hhm9bINNqa3@keats.ugrad.cs.ubc.ca>
- NNTP-Posting-Host: goanna.cs.rmit.edu.au
- X-Newsreader: NN version 6.5.0 #0 (NOV)
-
- c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
- >So what are we to do? It is true that: some operations cannot be expressed in
- >the ``obvious way'' on two's complement arithmetic. However, it is also true
- >that some operations that are expressed in the ``obvious way'' under two's
- >complement _fail_ under a sign and magnitude machines!
-
- Hokay, let's see what they are. They can't be _arithmetic_ operations.
-
- >The road goes both ways. Shall I give an example?
-
- Fine.
-
- >Under two's complement, I can convert a signed quantity into an unsigned
- >quantity such that the unsigned is _congruent_ to the signed, modulo a power of
- >two. I can do this just by chopping bits (if the unsigned quantity is as
- >narrow, or narrower than the signed type being converted). This is how standard
- >C defines the operation of converting a signed integral type into an unsigned
- >quantity that is narrower (K&R6.1, I think).
-
- There are several questions here:
- (a) is this really a useful thing to do?
- (b) if it is, can the *operation* be implemented with a symmetric
- representation of signed integers?
- (c) can it be implemented _just by masking_.
-
- To which questions my answers are
- (a) not before the triumph of twos complement it wasn't useful
- (b) yes
- (c) no
-
- Justification:
- (a) all I have to offer here is my personal experience. I have been using
- C on twos-complement machines since the beginning of 1980. I used an
- Algol dialect, Fortran, Pascal, and PL/I on a sign-and-magnitude machine
- for about 6 years before that. In all that time, I have never wanted
- to compute an unsigned number congruent to a signed number. There are
- lots of things I _have_ wanted, like fast gcd, but not that particular
- operation. It's one of those things you just don't think of when
- signed integers have more bits than unsigned ones. The absence of such
- an operation never got in the way of any problem solving I had to do.
-
- Well, thanks to the triumph of 32-bit twos-complement machines, I can
- think of one pretty important thing I would use this for: converting
- integers to XDR representation.
-
- (b) Let's take the B6700 as a concrete example, where the representation
- was
- word.[47:1] unused for compatibility with B5500
- word.[46:1] significand sign
- word.[45:1] exponent sign
- word.[39:6] base-8 biassed exponent
- word.[0:38] non-negative integer
- I hope I've got that right, it has been a long time. Integers have
- exponent 0.
-
- If I want a bit-string containing 40 bits that, if interpreted as an
- unsigned integer, is congruent to a particular integer, then
- DEFINE WORD = REAL;
-
- WORD PROCEDURE TO_UNSIGNED_40(X);
- INTEGER X; VALUE X;
- BEGIN
- IF X >= 0 THEN BEGIN
- TO_UNSIGNED_40 := X;
- END ELSE BEGIN
- WORD Y, LO, HI;
-
- Y := NOT(X);
- LO := Y.[0:20] + 1;
- HI := Y.[20:19] + LO.[20:1];
- TO_UNSIGNED_40 := LO.[0:20] & HI[0:20:20];
- END;
- END;
-
- I've forgotten the exact syntax for field insertion; the intent of
- the last assignment is
- "take the bottom 20 bits of LO with zero fill,
- and insert the bottom 20 bits of HI at offset 20".
-
- I have no B6700 to test this on, so I offer this only as an example
- of the kind of code that could do the job. If I want a smaller
- number of bits, it's quite easy. Suppose we know that X is in the
- range -7 to +7 and we want to get a 4-bit unsigned version:
-
- WORD PROCEDURE TO_UNSIGNED_4(X);
- INTEGER X; VALUE X;
- BEGIN
- IF ABS(X) > 7 THEN BEGIN
- ERROR("TO_UNSIGNED_X: DOMAIN ERROR");
- END ELSE
- IF X < 0 THEN BEGIN
- TO_UNSIGNED_4 := 16+X;
- END ELSE BEGIN
- TO_UNSIGNED_4 := X;
- END;
- END;
-
- The thing is, once you've got this bit string, what do you DO with it?
-
- (c) Admittedly, it isn't a single instruction, and it isn't done by masking,
- but "can get an unsigned version congruent mod a power of 2 by masking
- alone" is a _definition_ of twos-complement.
-
- The question is not whether you can do this, but whether it is a useful
- thing to do. All I can honestly say is that in
-
- If we were concerned with strange bit twiddling of use in specialised
- areas, the DSP people with their bit-reversed addressing for use in FFTs
- would win.
-
- >Don't tell me that two's complement doesn't have its neat and useful aspects!
- >It's a great idea that has been used successfully in many microprocessors!
-
- Well, so were memory-to-memory instructions a great idea (for a hacker) that
- were used successfully in many machines, so was hardware support for
- decimal arithmetic (which I really don't want to knock), so were 8-bit
- characters, so were base-and-limit registers, so was microcode. (The one
- time I had a potential use for WCS on a PDP-11/60 I couldn't find any
- explanation of how to use it, so I did without.)
-
- Xerox had (because they _needed_) a 16-bit character set by 1984 (which is
- when I met it, the XNS character set). The writing has been on the wall
- for 8-bit characters for some time, however neat and useful they may have
- been, and however successfully they may have been used. There are even
- alternatives to floating point which deserve exploration.
-
- In a world dominated by twos-complement machines, the practical utility of
- being one of the herd is beyond dispute. When I went from the B6700 to a
- PDP-11, I felt _sick_. I could appreciate how 2s complement made life
- easier for the architects, but it didn't help _me_, and "core dumped" was
- no substitute for the "Array subscript out of bounds at line #### called
- from ####" messages I was used to. (The first C compiler of my acquaintance
- got some of its 'long' operations wrong, so I had to fix the compiler. And
- 'long' was still what I was used to thinking of as 'short'. Sigh.)
-
- --
- The election is over, and Australia lost; the idjits elected _politicians_!
- Richard A. O'Keefe; http://www.cs.rmit.edu.au/~ok; RMIT Comp.Sci.
-